package org.ohos.rajawali3d.examples;


import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.eventhandler.EventHandler;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.recycler.CategoryAdapter;

public class ExamplesFragment extends Fraction implements SearchBar.QueryListener {

    static final String TAG = "ExamplesFragment";

    private CategoryAdapter adapter;
    private XRecyclerView mRecyclerView;
    public Component header;
    private CategoryAdapter<String> mAdapter;
    private java.util.ArrayList<String> listData;
    private int refreshTime = 0;
    private int times = 0;
    private final ohos.eventhandler.EventHandler handler = new EventHandler(ohos.eventhandler.EventRunner.getMainEventRunner());
    private ohos.app.Context mContext;

    public ExamplesFragment(ohos.app.Context context) {
        mContext = context;
    }

    @Override
    public void onStart(Intent savedInstanceState) {
        super.onStart(savedInstanceState);
        //TODO: OptionMenu setHasOptionsMenu(true);

        //adapter = new CategoryAdapter(getContext(), DataSet.getInstance().getCategories(), ResourceTable.Layout_fragment_examples);
        //adapter.setCategories(DataSet.getInstance().getCategories());

        initView();
    }

    @Override
    public Component onComponentAttached(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        Component view = inflater.parse(ResourceTable.Layout_fragment_examples, container, false);
        mRecyclerView = (XRecyclerView) view.findComponentById(ResourceTable.Id_recycler);
        mRecyclerView.setLayoutManager(new DirectionalLayoutManager());
        return view;
    }

    private void initView() {
        mRecyclerView.setRefreshProgressStyle(com.jcodecraeer.xrecyclerview.ProgressStyle.SysProgress);
        mRecyclerView.getDefaultRefreshHeaderView().setRefreshTimeVisible(true);
        mRecyclerView.getDefaultRefreshHeaderView().setIndicatorColor(ohos.agp.utils.Color.RED.getValue());

        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.SysProgress);
        mRecyclerView.setLoadingMoreProgressIndicatorColor(Color.RED.getValue());
        mRecyclerView.setArrowImageView(ResourceTable.Media_iconfont_downgrey);

        LayoutScatter scatter = LayoutScatter.getInstance(mContext);
        header = scatter.parse(ResourceTable.Layout_recyclerview_header, null, false);
        header.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ohos.agp.window.dialog.ToastDialog(getContext()).setText("这是头部").show();
            }
        });

        mRecyclerView.addHeaderView(header);
        mRecyclerView.getDefaultFootView().setLoadingHint("自定义加载中提示");
        mRecyclerView.getDefaultFootView().setNoMoreHint("自定义加载完毕提示");

        final int itemLimit = 5;
        mRecyclerView.setLimitNumberToCallLoadMore(2);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                handler.removeTask(refreshRunnable);
                handler.postTask(refreshRunnable, 1000);
            }

            @Override
            public void onLoadMore() {

                if (times < 2) {
                    new ohos.eventhandler.EventHandler(ohos.eventhandler.EventRunner.getMainEventRunner()).postTask(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < itemLimit; i++) {
                                listData.add("item" + (1 + listData.size()));
                            }
                            if (mRecyclerView != null) {
                                mRecyclerView.loadMoreComplete();
                                mAdapter.notifyDataChanged();
                            }
                        }
                    }, 1000);
                } else {
                    new ohos.eventhandler.EventHandler(ohos.eventhandler.EventRunner.getMainEventRunner()).postTask(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < itemLimit; i++) {
                                listData.add("item" + (1 + listData.size()));
                            }
                            if (mRecyclerView != null) {
                                mRecyclerView.setNoMore(true);
                                mAdapter.notifyDataChanged();
                            }
                        }
                    }, 1000);
                }
                times++;
            }
        });

        listData = new java.util.ArrayList<>();
        mAdapter = new CategoryAdapter<>(getContext(), listData, ResourceTable.Layout_list_item);

        mAdapter.setOnItemClickListener(new XRecyclerView.RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Component component, int position) {

                new ohos.agp.window.dialog.ToastDialog(getContext()).setText("nihao" + position).show();
            }
        });
        mRecyclerView.setItemProvider(mAdapter);
        mRecyclerView.refresh();
    }

    private final Runnable refreshRunnable = new Runnable() {
        @Override
        public void run() {
            refreshTime++;
            times = 0;
            listData.clear();
            for (int i = 0; i < 20; i++) {
                listData.add("item" + i + "after " + refreshTime + " times of refresh");
            }
            mAdapter.notifyDataChanged();
            if (mRecyclerView != null)
                mRecyclerView.refreshComplete();
        }
    };

    /*
    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_search, menu);

        MenuItem menuItemSearch = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menuItemSearch.getActionView();
        searchView.setOnQueryTextListener(this);
    }
*/
    @Override
    public boolean onQuerySubmit(String s) {
        adapter.getFilter().filter(s);
        return true;
    }

    @Override
    public boolean onQueryChanged(String s) {
        adapter.getFilter().filter(s);
        adapter.notifyDataChanged();
        return true;
    }
}
